//
//  UserAccount.swift
//  SinaWeibo
//
//  Created by apple on 15/11/10.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
/*

遵守NSCoding协议 实现两个协议方法

*/
class UserAccount: NSObject,NSCoding {
    
    //保存对象
    func saveAccount () {
        
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        print(path)
        
    }
    //向外界提供一个方法  解归档
    class func loadAccount() -> UserAccount? {
        
        //提供路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        print(path)
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            //判断token 是否过期
            if account.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                return account
            }
        }
        
        return nil
        
    }
    //用户的属性 需要的
    //用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    
    //过期日期
    var expires_Date: NSDate?
    //access_token的生命周期，单位是秒数
    var expires_in: NSTimeInterval = 0 {
        
        didSet {
            
            expires_Date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //当前授权用户的UID
    var uid: String?
    
    //用户头像地址 String
    var avatar_large: String?
    //用户名
    var name: String?
    
    //KVC构造
    init(dict:[String:AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)
        
    }
    //过滤掉没有写的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    
    //重写description
    
    override var description :String {
        
        let keys = ["access_token","expires_in","uid","avatar_large","name","expires_Date"]
        
        let dict = dictionaryWithValuesForKeys(keys)
        
        return dict.description
    }
    
    //MARK : NSCoding 的两个协议方法
    //解归档 就是将擦盘中的二级制数据转换成自定义对象
    required init?(coder aDecoder: NSCoder) {
        
        access_token =  aDecoder.decodeObjectForKey("access_token") as? String
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        name =  aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
    }
    
    //归档 就是将自定义对象转换成二进制数据存进磁盘中
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        
    //sb 多个空格                                   ⬇️这里
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    
}
